"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const configTool_1 = __importDefault(require("../../../com/configTool"));
const initDB_1 = __importDefault(require("../Util/initDB"));
const axios_1 = __importDefault(require("axios"));
const crypto_1 = __importDefault(require("crypto"));
const readFromDB_1 = __importDefault(require("../Util/readFromDB"));
const saveToDB_1 = __importDefault(require("../Util/saveToDB"));
const DBConnectTool_1 = __importDefault(require("../../../com/DBTool/DBConnectTool"));
const sleep_1 = __importDefault(require("../../sleep"));
let Loading = true;
const DBName = 'tile.googleapis.com';
const Cesium = require('../../../../ext/Cesium-1.130/index.js');
const defaultAccessToken = Cesium.Ion.defaultAccessToken;
let key = '';
let session = '';
async function getWallData(url, responseType = 'json') {
    return new Promise((resolve, reject) => {
        axios_1.default
            .get(url, {
            responseType: responseType,
            timeout: 1000,
            proxy: {
                protocol: 'http',
                host: '127.0.0.1',
                port: 7890
            }
        })
            .then(e => resolve(e.data))
            .catch(e => {
            console.log(e);
            resolve(null);
        });
    });
}
let Google3DTiles = {
    isRunning: false,
    async getRootJson(needGetToken = true) {
        while (this.isRunning) {
            await (0, sleep_1.default)(100);
        }
        console.log('Google3DTiles 初始化中...');
        await configTool_1.default.init();
        configTool_1.default.preservableONDB() && await (0, initDB_1.default)(DBName);
        await (0, initDB_1.default)(DBName);
        await DBConnectTool_1.default.openDB(DBName);
        let tileSetRoot = null;
        if (needGetToken) {
            const endpointUrl = 'https://api.cesium.com/v1/assets/2275207/endpoint?access_token=' + defaultAccessToken;
            try {
                const authority = await fetch(endpointUrl).then(e => e.json());
                const rootUrl = authority.options.url;
                const rootJson = await getWallData(rootUrl, 'json');
                const tileSetRootRelativeUri = rootJson.root.children[0].children[0].content.uri;
                key = new URL(rootUrl).searchParams.get('key') || '';
                session = new URL('https://tile.googleapis.com' + tileSetRootRelativeUri).searchParams.get('session') || '';
                tileSetRoot = rootJson;
            }
            catch (e) {
                console.log(endpointUrl);
                console.warn('获取资源令牌失败');
            }
        }
        if (!tileSetRoot) {
            const tileSetRootRelativeUri = '/v1/3dtiles/datasets/CgA/files/UlRPVEYuYnVsa21ldGFkYXRhLnBsYW5ldG9pZD1lYXJ0aCxidWxrX21ldGFkYXRhX2Vwb2NoPTk4MixwYXRoPSxjYWNoZV92ZXJzaW9uPTYsYWxpZ25tZW50X3ZlcnNpb249Uk9DS1RSRUVfOTgyX0dPT0dMRV9EQVRVTV8yMDI0MDUwOFQwNzUzWl9nZW5lcmF0ZWRfYXRfMjAyNDA3MTJUMTIwN1o.json';
            const { json } = await this.getJson(tileSetRootRelativeUri, true);
            tileSetRoot = json;
        }
        return { tileSetRoot };
    },
    async hasFile(uri) {
        const dbPath = uri.replaceAll('/', '\\');
        const dbTool = await DBConnectTool_1.default.openDB(DBName);
        const file = await dbTool.getFileByFullPath(dbPath);
        return !!file;
    },
    async getJson(uri, needSaveToDB = true) {
        const { err, buffer, dataSource } = await this.getBuffer(uri, needSaveToDB, true);
        if (buffer) {
            return { err, json: JSON.parse(buffer.toString()), dataSource };
        }
        else {
            return { err, json: null, dataSource };
        }
    },
    async getBuffer(uri, needSaveToDB = true, needCompress = false) {
        const dbPath = uri.replaceAll('/', '\\');
        let [err, buffer, dataSource] = await (0, readFromDB_1.default)({ DBName: DBName, relativePath: dbPath });
        if (buffer === null && key && session) {
            buffer = await getWallData(`https://tile.googleapis.com${uri}?session=${session}&key=${key}`, 'arraybuffer');
            if (buffer) {
                dataSource = 'Net';
                err = null;
            }
            else {
                err = '获取 tile.googleapis.com 文件失败';
            }
            if (needSaveToDB) {
                const md5Str = crypto_1.default.createHash('md5').update(buffer).digest('hex');
                if (md5Str) {
                    (0, saveToDB_1.default)({ DBName: DBName, relativePath: dbPath, buffer }, needCompress).then();
                }
            }
        }
        else if (key === '' || session === '') {
            err = '请先配置 key 和 session';
        }
        err && console.log(err);
        return { err, buffer, dataSource };
    }
};
exports.default = Google3DTiles;
